//references
var npnameinsrd = $("#npnameinsrd");
var npcpf = $("#npcpf");
var npbirthdate = $("#npbirthdate");
var npvehvendor = $("#npvehvendor");
var npvehimodel = $("#npvehimodel");
var nppaymentop = $("#nppaymentop");

var npstreetbil = $("#npstreetbil");
var npnumberbil = $("#npnumberbil");
var npbtcpyaddr = $("#npbtcpyaddr");

var npstreetrsk = $("#npstreetrsk");
var npnumberrsk = $("#npnumberrsk");

var npbtquoteag = $("#npbtquoteag");
var npbtnwquote = $("#npbtnwquote");
var npbtsubscrp = $("#npbtsubscrp");
// relevant model objects
var proposta = {
	cotacao : window.prevcotacao,
	formaPagamento : "",
	enderecoCobranca : {
		numero : 0,
		rua : ""
	},
	enderecoResidencia : {
		numero : 0,
		rua : ""
	}
};
// helper functions
function fillForm() {
	// data from quote
	npnameinsrd.val(proposta.cotacao.segurado.nome);
	npcpf.val(proposta.cotacao.segurado.cpf);
	// npbirthdate.datepicker("setDate", //
	// new Date(proposta.cotacao.segurado.dataNascimento));
	npbirthdate.datepicker("setDate",new Date(proposta.cotacao.segurado.dataNascimento));
	npbirthdate.datepicker("disable");
	
	npvehvendor.val(proposta.cotacao.veiculo.marca);
	npvehimodel.val(proposta.cotacao.veiculo.modelo);
	// underwrite-specific data
	nppaymentop.val(proposta.formaPagamento);
	npstreetbil.val(proposta.enderecoCobranca.rua);
	npnumberbil.val(proposta.enderecoCobranca.numero);
	npstreetrsk.val(proposta.enderecoResidencia.rua);
	npnumberrsk.val(proposta.enderecoResidencia.numero);

}
function fillBean() {
	proposta.formaPagamento = nppaymentop.val();
	proposta.enderecoCobranca.rua = npstreetbil.val();
	proposta.enderecoCobranca.numero = npnumberbil.val();
	proposta.enderecoResidencia.rua = npstreetrsk.val();
	proposta.enderecoResidencia.numero = npnumberrsk.val();
}

// events
npbtcpyaddr.click(function() {
	npstreetrsk.val(npstreetbil.val());
	npnumberrsk.val(npnumberbil.val());
});
npbtquoteag.click(function() {
	try{
		fillBean();
		window.prevproposta = proposta;
		centernav("cotacao/novacotacao");
	}catch(e){
		alert(e);
	}
});
npbtnwquote.click(function() {
	window.prevproposta = null;
	window.prevcotacao = null;
	centernav("cotacao/novacotacao");
});
npbtsubscrp.click(function() {
	try {
		fillBean();
		window.prevproposta = proposta;
		PropostaFacade.transmitirProposta(proposta, {
			callback:function(){
				//navigting to the end of the flow
				centernav("proposta/transmissao");
			}
		});
	} catch (e) {
		alert(e);
	}
});
// screen set-up
$("button").button();
$(".dtpicker").datepicker();
if (window.prevproposta) {
	proposta = window.prevproposta;
	window.prevproposta = null;
}
fillForm();